/**
 * Created by Ysssssss on 21/2/26.
 */
import dayjs from "dayjs";
import randomString from "@utils/randomString";
import * as qiniu from "qiniu-js";
import notify from "@utils/tip";
import {postAxios} from "@utils/api";


export const qiniuPicPath = 'http://bgp.bucket.bilinbili.com';

export async function uploadQiniuFile (file, firstKey, imageType, successCallback, failureCallback) {
    const key = `${firstKey}_${dayjs().format('YYYYMMDD')}_${randomString(12)}.${imageType}`
    const params = {
        bucket: 'bgp',
        key
    }
    const tokenJSON = await postAxios("/admin/api/vendor/v1/qiniu/upload-token", params)
    const token = tokenJSON.token
    // const token = "RZmWV1PvCF872j1_lyG8vbftOxQo3sJnQf2-qWIJ:q9aXpF6cOxqJ26opjjMnpT9le-s=:eyJzY29wZSI6ImJncCIsImRlYWRsaW5lIjoxNjIxNTg2MzAwfQ=="

    const observable = qiniu.upload(file, key, token)
    const subscription = observable.subscribe({
        error(err){
            notify(err.message, 'error')
            failureCallback && failureCallback()
        },
        complete(res){
            successCallback && successCallback({
                ...res,
                key: `${qiniuPicPath}/${res.key}`
            })
        }
    })
}

export async function uploadWechatImage(image){
    const res = await postAxios('/pay/wechat/image',{
        imageUrl:image
    })
    return res.media_id || false
}